package com.deer.wms.base.system.web.zhBusiness.in;

import com.alibaba.fastjson.JSONArray;
import com.deer.wms.base.system.finalUtil.AreaInfoId;
import com.deer.wms.base.system.finalUtil.BillState;
import com.deer.wms.base.system.finalUtil.BoxInfoState;
import com.deer.wms.base.system.finalUtil.SiteInfo;
import com.deer.wms.base.system.model.BillInRecord;
import com.deer.wms.base.system.model.bill.*;
import com.deer.wms.base.system.model.box.BoxInfo;
import com.deer.wms.base.system.model.box.BoxItem;
import com.deer.wms.base.system.model.box.BoxItemArea;
import com.deer.wms.base.system.model.item.ItemInfo;
import com.deer.wms.base.system.model.pda.*;
import com.deer.wms.base.system.model.zh.*;
import com.deer.wms.base.system.service.BillInRecordService;
import com.deer.wms.base.system.service.bill.*;
import com.deer.wms.base.system.service.box.BoxInfoService;
import com.deer.wms.base.system.service.box.BoxItemAreaService;
import com.deer.wms.base.system.service.box.IBoxItemService;
import com.deer.wms.base.system.service.item.IItemInfoService;
import com.deer.wms.base.system.service.pda.PdaActionRecordService;
import com.deer.wms.base.system.service.task.BillInTaskInfoService;
import com.deer.wms.base.system.service.task.ITaskInfoService;
import com.deer.wms.base.system.service.task.PickTaskService;
import com.deer.wms.base.system.service.task.TaskInfoHistoryService;
import com.deer.wms.base.system.service.ware.ICellInfoService;
import com.deer.wms.base.system.service.zh.*;
import com.deer.wms.base.system.util.JedisServiceUtil;
import com.deer.wms.base.system.util.RedisUtil;
import com.deer.wms.base.system.web.zhBusiness.business.QualityController;
import com.deer.wms.common.core.result.CommonCode;
import com.deer.wms.common.core.result.Result;
import com.deer.wms.common.core.result.ResultGenerator;
import com.deer.wms.common.utils.DateUtils;
import com.deer.wms.framework.util.ShiroUtils;
import com.deer.wms.system.domain.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Condition;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author 李帅辰
 * @date 2021/6/20 13:47
 */
@RestController
@RequestMapping("/zhPdaBillIn")
@Slf4j
public class ZhPdaBillInController {
    @Autowired
    PdaActionRecordService pdaActionRecordService;
    @Autowired
    private ITaskInfoService taskInfoService;//任务处理层

    @Autowired
    private TaskInfoHistoryService taskInfoHistoryService;

    @Autowired
    private IBillInDetailService billInDetailService;//入库单明细

    @Autowired
    private IBillInMasterService billInMasterService;

    @Autowired
    private IBoxItemService boxItemService;//组盘


    @Autowired
    private BoxInfoService boxInfoService;

    @Autowired
    private ElectricBoxItemService electricBoxItemService;

    @Autowired
    private IMaterialInfoService iMaterialInfoService;

    @Autowired
    private IMaterialDetailService iMaterialDetailService;

    @Autowired
    private BoxItemAreaService boxItemAreaService;

    @Autowired
    private BillInSapService billInSapService;

    @Autowired
    private BillInSapDetailService billInSapDetailService;

    @Autowired
    private ZhMesService zhMesService;

    @Autowired
    private IngotIocusService ingotIocusService;
    @Autowired
    private BoxItemExceptionService boxItemExceptionService;

    @Autowired
    private BillInRecordService billInRecordService;

    @Autowired
    private BillOutMesLogService billOutMesLogService;

    @Autowired
    private IItemInfoService itemInfoService;

    @Autowired
    private BillInSelectService billInSelectService;


    //查询待进入异常区的晶棒
    @GetMapping("/selectaBnormal")
    @ResponseBody
    public List<Bnormal> selectaBnormal(Bnormal bnormal) {
        List<Bnormal> bnormals = boxItemExceptionService.selectaBnormal(bnormal);
        return bnormals;
    }

    //查询当前晶棒的信息
    @GetMapping("/selectCrystalByid")
    @ResponseBody
    public List<Crystal> selectCrystalByid(MaterialDetail materialDetail) {
        List<Crystal> list = new ArrayList<>();
        List<MaterialDetail> materialDetails = iMaterialDetailService.selectCrystalByid(materialDetail);
        for (MaterialDetail m : materialDetails) {
            Crystal c = new Crystal();
            c.setParentMaterialId(m.getParentMaterialId());
            c.setMaterialProduct(m.getMaterialProduct());
            c.setWeight(Double.parseDouble(m.getWeight() + ""));
            list.add(c);
        }
        return list;
    }

    //查询此物料编码中可组托退货晶棒信息
    @GetMapping("/selectReturned")
    @ResponseBody
    public List<Crystal> selectReturned(MaterialDetail materialDetail) {
        List<Crystal> list = new ArrayList<>();
        List<MaterialDetail> materialDetails = iMaterialDetailService.selectReturned(materialDetail);
        for (MaterialDetail m : materialDetails) {
            Crystal c = new Crystal();
            c.setParentMaterialId(m.getParentMaterialId());
            c.setMaterialProduct(m.getMaterialProduct());
            c.setWeight(Double.parseDouble(m.getWeight() + ""));
            list.add(c);
        }
        return list;
    }

    //查询所有未完成的退货单据信息
    @GetMapping("/selectUnfinished")
    @ResponseBody
    public List<PdaweightDto> Pdatray(Pdaweight pdaweight) {
        if (pdaweight.getBill_in_sap_detail_id() == -1) {
            pdaweight = new Pdaweight();
            pdaweight.setBill_in_sap_detail_id(null);
        }
        List<PdaweightDto> electricBoxDetails = electricBoxItemService.selectUnfinished(pdaweight);
        return electricBoxDetails;
    }

    //已经入库的托盘查询
    @GetMapping("/Pdatray")
    @ResponseBody
    public List<BoxInMainList> Pdatray(BoxInMainList boxInMainList) {
        List<BoxInMainList> boxInMainLists = electricBoxItemService.selectPdatray(boxInMainList);
        return boxInMainLists;
    }

    //根据托号查晶棒信息
    @PostMapping("/PdaIngot")
    @ResponseBody
    public List<ElectricBoxDetail> Ingot(String torr) {
        Condition condition = new Condition(ElectricBoxDetail.class);
        condition.createCriteria().andCondition("Torr=", torr);
        List<ElectricBoxDetail> byCondition = electricBoxItemService.findByCondition(condition);
        return byCondition;
    }

    /**
     * 1：手持机收货时。扫描托盘条码，查询电子箱单信息
     * @return com.deer.wms.base.system.model.zh.ElectricBoxDetail
     * @author 李帅辰
     * @date 2021/6/20 13:53
     */
    @GetMapping("/ScanDelivery")
    @ResponseBody
    public List<BoxInMainList> pdaScanDelivery(BoxInMainList boxInMainList) throws Exception {
        List<BoxInMainList> list = new ArrayList();
        List<BoxInMainList> electricBoxDetailByTorr = electricBoxItemService.findElectricBoxDetailByTorr(boxInMainList);
        for (BoxInMainList b : electricBoxDetailByTorr) {
            BoxInMainList bm = new BoxInMainList();
            bm.setTorr(b.getTorr());
            bm.setVText(b.getVText());
            bm.setCreatetime(b.getCreatetime());
            bm.setVBELN(b.getVBELN());
            list.add(bm);
        }

        return list;
    }


    /**
     * 2:手持机确认收货，WMS先查询该托对应的交货单号，然后将晶棒明细新增至该单据下,将晶棒库存信息，添加至box_item和BoxItemArea
     *
     * @param name            用户姓名
     * @param torr            托号
     * @param WareId          库位二维码
     * @param user_id         账户对应user_id
     * @param pdaActionRecord 操作记录
     * @return com.deer.wms.common.core.result.Result
     * @author 李帅辰
     * @date 2021/6/20 13:56
     */
    @PostMapping("/PdaCommitDelive")
    @ResponseBody
    @Transactional
    public Result PdaCommitDelive(String torr, String WareId, String name, Integer user_id, PdaActionRecord pdaActionRecord) {
        //以晶棒编码查询电子箱单
        ElectricBoxDetail electricBoxDetail = new ElectricBoxDetail();
        electricBoxDetail.setTorr(torr);

        List<ElectricBoxDetail> electricBoxDetailList = electricBoxItemService.selectElectricBoxDetail(electricBoxDetail);//得到该托盘上所有的物料批次
        for (ElectricBoxDetail boxDetail : electricBoxDetailList) {
            boxDetail.setTorr(torr);
            List<ElectricBoxDetail> electricBoxDetails = electricBoxItemService.selectAll(boxDetail);//以托盘、物料、批次查询此数据下的晶棒信息

            if (null == electricBoxDetails) {
                return ResultGenerator.genFailResult(CommonCode.ELECTRIC_ERROR);
            }

            MaterialInfo materialInfo2 = new MaterialInfo();
            materialInfo2.setCarrierId(torr);
            if (null != iMaterialInfoService.findMaterialInfo(materialInfo2)) {
                return ResultGenerator.genFailResult(CommonCode.ELECTRIC_IN_ERROR);
            }

            for (ElectricBoxDetail electricBoxDetailByTorr : electricBoxDetails) {
                String weight = electricBoxDetailByTorr.getWeight();//重量
                String t_weigh = electricBoxDetailByTorr.getT_Weigh();//理论重量
                String barCode = electricBoxDetailByTorr.getBarCode();//物料编码
                String conductive = electricBoxDetailByTorr.getConductive();//电子箱单携带的此托的的收货单号

                //此处需要添加SAP单据号校验,不存在则返回false
                boolean billInSapByvBELN = billInSapService.findBillInSapByvBELN(conductive);
                if (billInSapByvBELN) {
                    //新增入库单,然后再此校验单据。若已经新增过，就返回billId
                    Integer billId = null;
                    BillInMaster billInMaster1 = billInMasterService.selectBillInMasterByBillNo(conductive);
                    if (null == billInMaster1) {
                        BillInMaster billInMaster = new BillInMaster();
                        //billInMaster.setAspNo(conductive);
                        billInMaster.setBillNo(conductive);
                        billInMaster.setMemo("pda扫码收货");
                        //billInMaster.setWareId(Integer.parseInt(WareId));
                        billInMaster.setCreateUserName(name);
                        billInMaster.setCreateUserId(user_id);
                        billInMaster.setState(BillState.BILLIN_STATE_TWO);
                        int i = billInMasterService.insertBillInMaster(billInMaster);
                        billId = billInMaster.getBillId();
                    } else {
                        billId = billInMaster1.getBillId();
                    }
                    int billInSelectId;
                    BillInSelect inSelect = billInSelectService.findBillInSelect(electricBoxDetail);
                    if (null == inSelect) {
                        //新增bill_in_select
                        ElectricBoxDetail detail = electricBoxItemService.findElectricBoxDetail(electricBoxDetail);
                        BillInSelect billInSelect = new BillInSelect();
                        billInSelect.setBillId(billId);
                        billInSelect.setBarCode(detail.getBarCode());
                        billInSelect.setSpec(detail.getRemark5());
                        billInSelect.setMaterialBatchId(detail.getF_Degree());
                        billInSelect.setQuantity(Float.parseFloat(detail.getWeight()));
                        billInSelect.setCarrierId(detail.getTorr());
                        billInSelectService.saveBillInSelect(billInSelect);
                        billInSelectId=billInSelect.getBillInSelectId();
                    }else {
                        billInSelectId = inSelect.getBillInSelectId();
                    }
                    //新增bill_in_material
                    MaterialInfo materialInfo = new MaterialInfo();
                    materialInfo.setMaterialBatchId(electricBoxDetailByTorr.getF_Degree());//批次
                    materialInfo.setCarrierId(electricBoxDetailByTorr.getTorr());//托号
                    MaterialInfo materialInfo1 = iMaterialInfoService.findMaterialInfo(materialInfo);
                    if (null == materialInfo1) {
                        materialInfo.setState(3);
                        int i = iMaterialInfoService.saveMaterialInfo(materialInfo);
                    }

                    String workShop = electricBoxDetailByTorr.getWorkShop();//晶棒组合内容
                    String[] split = workShop.split(";");
                    for (String s : split) {
                        IngotIocus ingotIocus = new IngotIocus();
                        ingotIocus.setMaterialId(electricBoxDetailByTorr.getCoding());
                        ingotIocus.setLocale(SiteInfo.BILLIN_FRIST);
                        ingotIocus.setType(1);
                        ingotIocusService.saveIngotIocus(ingotIocus);
                        ingotIocus.setType(3);
                        ingotIocusService.saveIngotIocus(ingotIocus);
                        //新增bill_in_material_detail

                        String barCode1 = electricBoxDetailByTorr.getBarCode();
                        ItemInfo itemInfo = itemInfoService.slectItemInfoByCode(barCode1);
                        //String model = itemInfo.getModel();
                        //List<String> strings = Arrays.asList(model.split("/"));
                        //strings.get(1);


                        MaterialDetail materialDetail = new MaterialDetail();
//                        materialDetail.setResistance(strings.get(6));
//                        materialDetail.setMaterialPN(strings.get(2));
//                        materialDetail.setCarrierId(electricBoxDetailByTorr.getTorr());//托号
//                        materialDetail.setParentMaterialId(electricBoxDetailByTorr.getCoding());//主棒编码
//                        materialDetail.setMaterialId(electricBoxDetailByTorr.getCoding());
//                        materialDetail.setSpecId(strings.get(5));//单晶规格
//                        materialDetail.setDiameter(Float.parseFloat(strings.get(4)));
//                        materialDetail.setSideLength(Float.parseFloat(strings.get(3)));
//                        materialDetail.setStockInLength(electricBoxDetailByTorr.getR_Long());//入库单晶长度
                        materialDetail.setBrickLength(electricBoxDetailByTorr.getD_Long());//单晶长度
                        materialDetail.setWeight(Float.parseFloat(electricBoxDetailByTorr.getWeight()));//入库重量
                        materialDetail.setMaterialProduct(electricBoxDetailByTorr.getBarCode());//物料编码
                        materialDetail.setMaterialCategory(electricBoxDetailByTorr.getCustomerProfile());//单晶等级
                        materialDetail.setMaterialId(s);//单晶编码
                        //此处判断单晶组合内容中‘;’出现的次数 ，1次为整棒，2次为拼帮
                        if (workShop.indexOf(";") == workShop.lastIndexOf(";")) {
                            materialDetail.setAssemblyType(1);//拼棒类型
                        } else {
                            materialDetail.setAssemblyType(2);//拼棒类型
                        }
                        int i1 = iMaterialDetailService.insertMaterialDetail(materialDetail);
                        //新增入库单明细
                        BillInDetail billInDetail = new BillInDetail();
                        billInDetail.setBillId(billId);
                        //billInDetail.setBillInMaterialDetailId(materialDetail.getId());
                        //billInDetail.setBillInSelectId(billInSelectId);
                        Boolean aBoolean = billInDetailService.saveBillInDetail(billInDetail);
                        //新增库存信息
                        BoxItem boxItem = new BoxItem();
                        //更新库存
                        boxItem.setBoxCode("TP" + electricBoxDetailByTorr.getCoding());
                        boxItem.setBatch(electricBoxDetailByTorr.getF_Degree());
                        //boxItem.setItemCode("");
                        boxItem.setBillInDetailId(billInDetail.getBillInDetailId());
                        boxItem.setWareId(1220);
                        boxItem.setState(0);
                        //boxItem.setQuantity(1);
                        //boxItem.setItemCode(electricBoxDetailByTorr.getBarCode());
                        //boxItem.setBillInMaterialDetailId(materialDetail.getId());
                        BoxItem verifyBoxItem = boxItemService.verifyBoxItem(boxItem);

                        if (null == verifyBoxItem) {
                            int i2 = boxItemService.insertBoxItem(boxItem);
                        }
                        BoxInfo boxInfo = new BoxInfo();
                        boxInfo.setBoxCode("TP" + electricBoxDetailByTorr.getCoding());
                        boxInfo.setBoxState(BoxInfoState.BOXINFO_STATE_ZERO);//0-不在货位 1-货位上  2-任务中
                        boxInfo.setHasGoods(BoxInfoState.BOXINFO_STATE_ZERO);//   0-无货 1-有货
                        boxInfo.setBoxType(2);//类型：1-料箱  2-托盘
                        boxInfoService.save(boxInfo);

                        BoxItemArea boxItemArea = new BoxItemArea();
                        boxItemArea.setAreaInfoId(AreaInfoId.AREAINFO_TURNOVER);
                        boxItemArea.setBoxItemId(boxItem.getId().intValue());
                        int i2 = boxItemAreaService.saveBoxItemArea(boxItemArea);


                        //同步入库报表
                        BillInRecord billInRecord = new BillInRecord();
                        billInRecord.setBillInDetailId(billInDetail.getBillInDetailId());//出库单id
                        billInRecord.setAcceptQuantity(1);//入库数量
                        billInRecord.setBoxCode(electricBoxDetailByTorr.getTorr());
                        billInRecord.setBatch(electricBoxDetailByTorr.getF_Degree());
                        BillInRecord verifyBillInRecord = billInRecordService.verifyBillInRecord(billInRecord);
                        if (null == verifyBillInRecord) {
                            int i = billInRecordService.insertBillInRecord(billInRecord);
                        }
                        //修改SAP单据以及明细的状态
                        BillInSapDetail billInSapDetail = new BillInSapDetail();
                        //billInSapDetail.setVBELN(conductive);//交货单号
                        //billInSapDetail.setMATNR(barCode);
                        BillInSapDetail billInSapDetail1 = billInSapDetailService.findBillInSapDetail(billInSapDetail);
                        //float remain = billInSapDetail1.getRemain();//应该入库的重量
                        float d = Float.parseFloat(weight);
                        //float nowLgmng = billInSapDetail1.getLGMNG() + d;//现在入库的
                        billInSapDetail.setCarryState(1);
//                        if (nowLgmng >= remain) {
//                            billInSapDetail.setReceiptState(3);
//                        } else {
//                            billInSapDetail.setReceiptState(2);
//                        }
//                        billInSapDetail.setLGMNG(nowLgmng);
                        int i3 = billInSapDetailService.updateBillInSapDetail(billInSapDetail);

                        BillInSapDetailCriteria billInSapDetailCriteria = new BillInSapDetailCriteria();
                        billInSapDetailCriteria.setVBELN(conductive);//交货单号
                        billInSapDetailCriteria.setMATNR(barCode);
                        List<BillInSapDetail> billInSapDetailList = billInSapDetailService.findBillInSapDetailListByVBELN(billInSapDetailCriteria);
                        int size = billInSapDetailList.size();
                        int sum = 0;
                        for (BillInSapDetail inSapDetail : billInSapDetailList) {
//                            float lgmng = inSapDetail.getLGMNG();
//                            float remain1 = inSapDetail.getRemain();
//                            if (lgmng == remain1) {
//                                sum = sum + 1;
//                            }
                        }
                        BillInSap billInSap = new BillInSap();
                        if (size == sum) {
                            billInSap.setBillState(3);
                        } else {
                            billInSap.setBillState(2);
                        }
                        //billInSap.setVBELN(conductive);
                        billInSapService.updateBillInSap(billInSap);
                    }
                    //MES同步信息
                    //  this.SysnMes(torr);
                } else {
                    Result result = ResultGenerator.genFailResult(CommonCode.ZHPDABILLIN_ERROR);
                    if (result.getCode() == 0) {
                        //TODO 记录日志
                        String time = DateUtils.getTime();
                        String substring = time.substring(0, 19);
                        pdaActionRecord.setCreateTime(substring);
                        pdaActionRecordService.save(pdaActionRecord);
                    }
                    return result;
                }
            }
            Result result = ResultGenerator.genSuccessResult();
            if (result.getCode() == 0) {
                //TODO 记录日志
                String time = DateUtils.getTime();
                String substring = time.substring(0, 19);
                pdaActionRecord.setCreateTime(substring);
                pdaActionRecordService.save(pdaActionRecord);
            }
            return result;
        }
        return ResultGenerator.genFailResult(CommonCode.ELECTRIC_ERROR);
    }


    /**
     * @return boolean
     * @author 李帅辰
     * @date 2021/6/27 22:55
     */
    @PostMapping("/SysnMes")
    @ResponseBody
    public boolean SysnMes(String carrierId) {
        ElectricBoxDetail electricBoxDetail = new ElectricBoxDetail();
        electricBoxDetail.setTorr(carrierId);
        List<ElectricBoxDetail> electricBoxDetails = electricBoxItemService.selectAll(electricBoxDetail);
        List<MaterialInfo> billOutParentMaterialIdList = iMaterialInfoService.findBillOutParentMaterialIdList(carrierId);
        if (null != billOutParentMaterialIdList) {
            for (MaterialInfo materialInfo : billOutParentMaterialIdList) {
                ZhBox zhBox = new ZhBox();
                List<ZhItem> materialInfoList = new ArrayList<>();
                ZhItem zhItem = new ZhItem();
                //mes数据格式的上半部分
                zhBox.setMaterialBatchId(materialInfo.getMaterialBatchId());//批次
                zhBox.setCarrierId(materialInfo.getCarrierId());//托好
                zhBox.setParentMaterialId(materialInfo.getParentMaterialId());//物料编码
                zhBox.setMaterialProduct(materialInfo.getMaterialProduct());
                zhBox.setMaterialQuality("Normal");
                //materialInfo部分
                MaterialDetailCriteria materialDetailCriteria = new MaterialDetailCriteria();
                materialDetailCriteria.setCarrierId(carrierId);
                List<MaterialDetail> list = iMaterialDetailService.findList(materialDetailCriteria);
                for (MaterialDetail materialDetail : list) {
                    zhItem = new ZhItem();
                    zhItem = new ZhItem();
                    BeanUtils.copyProperties(materialDetail, zhItem);

                    materialInfoList.add(zhItem);
                }
                zhBox.setMaterialInfo(materialInfoList);
                String s = zhMesService.SysnBoxItem(zhBox);
                log.info("MES入库同步库存信息"+s);
                ZhResult zhResult= JSONArray.parseObject(s, ZhResult.class);//mes的返回值
                Integer code = zhResult.getCode();
                BillOutMesLog billOutMesLog = new BillOutMesLog();
                billOutMesLog.setCode(code.toString());
                billOutMesLog.setJson(JSONArray.toJSONString(zhBox));
                billOutMesLog.setOp("NMMaterialInfo");
                billOutMesLog.setResultJson(s);
                billOutMesLogService.saveBillOutMesLog(billOutMesLog);
            }
        }
        return false;
    }

    /**
     * 托盘进入拆垛区，扫码确认
     *
     * @return com.deer.wms.common.core.result.Result
     * @author 李帅辰
     * @date 2021/6/25 9:42
     */
    @PostMapping("/PdaScanDeliveryBreak")
    @ResponseBody
    @Transactional
    public Result PdaScanDeliveryBreak(String torr) {
        List<MaterialDetail> materialDetailList = iMaterialDetailService.findMaterialDetailList(torr);
        if (null == materialDetailList) {
            return ResultGenerator.genFailResult(CommonCode.ELECTRIC_ERROR);
        }
        for (MaterialDetail materialDetail : materialDetailList) {
            String materialId = materialDetail.getMaterialId();
            BoxItem idByCarrierId = boxItemService.findIdByCarrierId(materialId);

            Long id = idByCarrierId.getId();
            BoxItemArea boxItemArea = new BoxItemArea();
            boxItemArea.setBoxItemId(id.intValue());
            boxItemArea.setAreaInfoId(AreaInfoId.AREAINFO_TEMPORARY_AREA);
            int i = boxItemAreaService.updateBoxItemArea(boxItemArea);

            IngotIocus ingotIocus = new IngotIocus();
            ingotIocus.setMaterialId(materialId);
            ingotIocus.setLocale(SiteInfo.BILLIN_SECOND);
            ingotIocus.setType(3);
            ingotIocusService.saveIngotIocus(ingotIocus);
        }
        return ResultGenerator.genSuccessResult();
    }


    /**
     * 取消入库
     *
     * @return com.deer.wms.common.core.result.Result
     * @author 李帅辰
     * @date 2021/6/27 20:58
     */
    @PostMapping("/CancelBillIn")
    @ResponseBody
    @Transactional
    public Result CancelBillIn(String torr) {
        List<MaterialDetail> materialDetailList = iMaterialDetailService.findMaterialDetailList(torr);
        if (null == materialDetailList) {
            return ResultGenerator.genFailResult(CommonCode.ELECTRIC_ERROR);
        }
        for (MaterialDetail materialDetail : materialDetailList) {
            BoxItem idByCarrierId = boxItemService.findIdByCarrierId(materialDetail.getMaterialId());
            Long id = idByCarrierId.getId();
            int i = boxItemAreaService.delectBoxItemArea(id.intValue());
            iMaterialDetailService.delectMaterialDetail(materialDetail.getId());
        }
        iMaterialInfoService.delectMaterialInfo(torr);
        Result result = ResultGenerator.genSuccessResult();
        return result;
    }
}
